﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;
using Entities;

namespace ShoppingCartWEB
{
    public partial class Products : System.Web.UI.Page
    {
        public IEnumerable<Product> list;
        private CategoryManager cm = new CategoryManager();
        private ProductManager man = new ProductManager();
        private ShoppingCard card = new ShoppingCard();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["cid"] != null)
                {
                    int cid = Int32.Parse(Request.QueryString["cid"]);
                    list = man.GetProductByCategory(cid);
                    dlProduct.DataSource = list;
                    dlProduct.DataBind();
                }
                else if (Request.QueryString["sid"] != null)
                {
                    int sid = Int32.Parse(Request.QueryString["sid"]);
                    list = man.GetProductBySupplier(sid);
                    dlProduct.DataSource = list;
                    dlProduct.DataBind();
                }
                else
                {
                    list = man.AllProduct();
                    dlProduct.DataSource = list;
                    dlProduct.DataBind();
                }
               
                if (Request.QueryString["pr"] != null)
                {
                    int nPrice = Int32.Parse(Request.QueryString["pr"]);
                    if (nPrice == 1)
                    {
                        list = man.SearchProductByPrice(0, 5000000);
                        dlProduct.DataSource = list;
                        dlProduct.DataBind();
                    }
                    else if(nPrice == 2)
                    {
                        list = man.SearchProductByPrice(5000000, 10000000);
                        dlProduct.DataSource = list;
                        dlProduct.DataBind();
                    }
                    else if (nPrice == 3)
                    {
                        list = man.SearchProductByPrice(10000000, 20000000);
                        dlProduct.DataSource = list;
                        dlProduct.DataBind();
                    }
                    else if (nPrice == 4)
                    {
                        list = man.SearchProductByPrice(200000000, 100000000);
                        dlProduct.DataSource = list;
                        dlProduct.DataBind();
                    }
                    else
                    {
                        list = man.AllProduct();
                        dlProduct.DataSource = list;
                        dlProduct.DataBind();
                    }
                }

                if (Request.QueryString["caid"] != null)
                {
                    string txtSearch = Request.QueryString["txt"];

                    Response.Redirect("Default.aspx");                 
                }
            }
        }

        protected void dlProduct_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "AddToCart")
            {
                int id = (int)dlProduct.DataKeys[e.Item.ItemIndex];
                Response.Write("xac nhan day la: " + id);
                //add To Cart
                card.Cards = GetCart();
                card.AddProduct(man.GetProductById(id));
                Session["Cart"] = card.Cards;
                Response.Redirect("ShoppingCart.aspx");
            }
        }

        private Dictionary<Product, int> GetCart()
        {
            if (Session["Cart"] == null)
                Session.Add("Cart", new Dictionary<Product, int>());
            return (Dictionary<Product, int>)Session["Cart"];
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            dlProduct.DataSource = man.SeachrProductByName(TextBox1.Text);
            dlProduct.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            list = man.AllProduct();
            dlProduct.DataSource = list;
            dlProduct.DataBind();
        }
    }
}